package com.example.blogdemo.common.interceptor;

import com.example.blogdemo.common.Utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userToken = request.getHeader("user_token");

        log.info("userToken:{}",userToken);
        if (userToken == null) {
            log.error("userToken == null");
            response.setStatus(401);
            return false;
        }
        Claims claims = JwtUtils.parseJwt(userToken);
        if (claims == null) {
            log.error("claims == null");
            response.setStatus(401);

            return false;
        }


        log.info("没有拦截");
        return true;

    }
}
